home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 June / CHIP Haziran 2001.iso / prog / haziran / 19 / setup.exe / data.z / wddebug.c < prev    next >
C/C++ Source or Header  |  2001-04-11  |  13KB  |  423 lines

  1. ////////////////////////////////////////////////////////////////
  2. // File - WDDEBUG.C
  3. //
  4. // A utility that turns WinDriver's debug mode on and off.
  5. // Debug mode checks every IO and memory transfer command,
  6. // making sure it fits in with the card's registered
  7. // resources. If an illegal command is given, WinDriver
  8. // will ignore the command and show a warning message on
  9. // screen. Debug mode slows down transfer operations, 
  10. // therefore it should be used only in the development proccess.
  11. // Running this command without parameters will print the
  12. // version of WinDriver installed.
  13. // 
  14. // If debug mode is set, this utility also enables you to print
  15. // out debug messages from the kernel, by the dump command.
  16. // 
  17. ////////////////////////////////////////////////////////////////
  18.  
  19. #include "../../include/windrvr.h"
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if defined(UNIX) || defined(OS2)
  23.     #include <unistd.h>
  24.     #include <fcntl.h>
  25. #elif !defined(WINCE)
  26.   #include <conio.h>
  27. #endif
  28. #if !defined(WINCE)
  29.   #include <time.h>
  30. #else
  31.     extern time_t time();
  32. #endif
  33. #if defined(UNIX) || defined(WINCE)
  34.     int _stricmp(const char *s1, const char *s2)
  35.     {
  36.         int i;
  37.         for (i=0; s1[i] && s2[i] && toupper(s1[i])==toupper(s2[i]); i++);
  38.         if (s1[i]=='\0' && s2[i]=='\0') return 0;
  39.         return -1;
  40.     }
  41. #endif
  42. #if defined(UNIX) || defined(OS2)
  43.     void Sleep(int nMilli)
  44.     {
  45.         #if defined(VXWORKS)
  46.             taskDelay( 1 );
  47.         #elif defined(OS2)
  48.             DosSleep(nMilli);
  49.         #else
  50.             usleep(nMilli * 1000);
  51.         #endif
  52.     }
  53. #endif
  54.  
  55. void Usage()
  56. {
  57.     printf ("This program sets debug mode of " WD_PROD_NAME " on or off\n");
  58.     printf ("WDDEBUG off      - sets debugging mode OFF\n");
  59.     printf ("WDDEBUG on       - sets debugging mode ON at ERROR level, for all sections\n");
  60.     printf ("WDDEBUG on [level] [sections...] - sets debugging mode on for specific sections\n");
  61.     printf ("WDDEBUG status   - prints the current version of " WD_PROD_NAME " and debug status\n");
  62.     printf ("WDDEBUG dump     - prints out debug messages\n");
  63.     printf ("\n");
  64.     printf ("    level - ERROR, WARN, INFO, TRACE\n");
  65.     printf ("    sections - ALL, IO, MEM, INT, PCI, PCMCIA, ISAPNP, \n");
  66.     printf ("               DMA, KER_PLUG, MISC, LICENSE, CARD_REG\n");
  67.     printf ("               ISAPNP, PCMCIA, KER_PLUG, KER_DRV\n");
  68.     printf ("\n");
  69.     printf ("Example: Turn on and view debugging for PCI and DMA routines, at info level\n");
  70.     printf ("    WDDEBUG on info \"pci dma\"\n");
  71.     printf ("    WDDEBUG dump\n");
  72. }
  73.  
  74. BOOL LevelToDword(char *sLevel, DWORD *pdwLevel)
  75. {
  76.     DWORD dwLevel = (DWORD)-1;
  77.     if (_stricmp(sLevel,"ERROR")==0) dwLevel = D_ERROR;
  78.     if (_stricmp(sLevel,"WARN")==0) dwLevel = D_WARN;
  79.     if (_stricmp(sLevel,"INFO")==0) dwLevel = D_INFO;
  80.     if (_stricmp(sLevel,"TRACE")==0) dwLevel = D_TRACE;
  81.     if (_stricmp(sLevel,"OFF")==0) dwLevel = D_OFF;
  82.     if (dwLevel==(DWORD)-1) return FALSE;
  83.     *pdwLevel = dwLevel;
  84.     return TRUE;
  85. }
  86.  
  87. char *LevelToString(DWORD dwLevel)
  88. {
  89.     if (dwLevel==D_OFF) return "OFF";
  90.     if (dwLevel==D_ERROR) return "ERROR";
  91.     if (dwLevel==D_WARN) return "WARN";
  92.     if (dwLevel==D_INFO) return "INFO";
  93.     if (dwLevel==D_TRACE) return "TRACE";
  94.     return "";
  95. }
  96.  
  97. BOOL SectionToDword(char *sSection, DWORD *pdwSection)
  98. {
  99.     char tokBuf[1024];
  100.     char *tok;
  101.     *pdwSection = 0;
  102.     strcpy (tokBuf, sSection);
  103.     for (tok = strtok(tokBuf, " "); tok; tok = strtok(NULL, " "))
  104.     {
  105.         if (_stricmp(tok, "ALL")==0) *pdwSection |= S_ALL;
  106.         else if (_stricmp(tok, "IO")==0)        *pdwSection |= S_IO;
  107.         else if (_stricmp(tok, "MEM")==0)       *pdwSection |= S_MEM;
  108.         else if (_stricmp(tok, "INT")==0)       *pdwSection |= S_INT;
  109.         else if (_stricmp(tok, "PCI")==0)       *pdwSection |= S_PCI;
  110.         else if (_stricmp(tok, "DMA")==0)       *pdwSection |= S_DMA;
  111.         else if (_stricmp(tok, "ISAPNP")==0)    *pdwSection |= S_ISAPNP;
  112.         else if (_stricmp(tok, "PCMCIA")==0)    *pdwSection |= S_PCMCIA;
  113.         else if (_stricmp(tok, "KER_PLUG")==0)  *pdwSection |= S_KER_PLUG;
  114.         else if (_stricmp(tok, "MISC")==0)      *pdwSection |= S_MISC;
  115.         else if (_stricmp(tok, "LICENSE")==0)   *pdwSection |= S_LICENSE;
  116.         else if (_stricmp(tok, "ISAPNP")==0)    *pdwSection |= S_ISAPNP;
  117.         else if (_stricmp(tok, "PCMCIA")==0)    *pdwSection |= S_PCMCIA;
  118.         else if (_stricmp(tok, "KER_PLUG")==0)  *pdwSection |= S_KER_PLUG;
  119.         else if (_stricmp(tok, "CARD_REG")==0)  *pdwSection |= S_CARD_REG;
  120.         else if (_stricmp(tok, "KER_DRV")==0)   *pdwSection |= S_KER_DRV;
  121.         else if (tok[0]=='0' && toupper(tok[1])=='x')
  122.         {
  123.             DWORD dwSection;
  124.             sscanf(tok+2, "%x", &dwSection);
  125.             *pdwSection |= dwSection;
  126.         }
  127.         else return FALSE;
  128.     }
  129.  
  130.     return TRUE;
  131. }
  132.  
  133. char *SectionToString(DWORD dwSection)
  134. {
  135.     static char sSection[1024];
  136.  
  137.     sSection[0] = '\0';
  138.     if (dwSection==S_ALL) 
  139.     {
  140.         strcat (sSection, "ALL ");
  141.         return sSection;
  142.     }
  143.     if (dwSection & S_IO)       { strcat (sSection, "IO ");       dwSection &= ~S_IO; }
  144.     if (dwSection & S_MEM)      { strcat (sSection, "MEM ");      dwSection &= ~S_MEM; }
  145.     if (dwSection & S_INT)      { strcat (sSection, "INT ");      dwSection &= ~S_INT; }
  146.     if (dwSection & S_PCI)      { strcat (sSection, "PCI ");      dwSection &= ~S_PCI; }
  147.     if (dwSection & S_DMA)      { strcat (sSection, "DMA ");      dwSection &= ~S_DMA; }
  148.     if (dwSection & S_ISAPNP)   { strcat (sSection, "ISAPNP ");   dwSection &= ~S_ISAPNP; }
  149.     if (dwSection & S_PCMCIA)   { strcat (sSection, "PCMCIA ");   dwSection &= ~S_PCMCIA; }
  150.     if (dwSection & S_KER_PLUG) { strcat (sSection, "KER_PLUG "); dwSection &= ~S_KER_PLUG; }
  151.     if (dwSection & S_MISC)     { strcat (sSection, "MISC ");     dwSection &= ~S_MISC; }
  152.     if (dwSection & S_LICENSE)  { strcat (sSection, "LICENSE ");  dwSection &= ~S_LICENSE; }
  153.     if (dwSection & S_ISAPNP)   { strcat (sSection, "ISAPNP ");   dwSection &= ~S_ISAPNP; }
  154.     if (dwSection & S_PCMCIA)   { strcat (sSection, "PCMCIA ");   dwSection &= ~S_PCMCIA; }
  155.     if (dwSection & S_KER_PLUG) { strcat (sSection, "KER_PLUG "); dwSection &= ~S_KER_PLUG; }
  156.     if (dwSection & S_CARD_REG) { strcat (sSection, "CARD_REG "); dwSection &= ~S_CARD_REG; }
  157.     if (dwSection & S_KER_DRV)  { strcat (sSection, "KER_DRV ");  dwSection &= ~S_KER_DRV; }
  158.  
  159.     if (dwSection)
  160.         sprintf (sSection+strlen(sSection), "0x%08x", dwSection);
  161.     return sSection;
  162. }
  163.  
  164. void Print_version(WD_VERSION *pVer)
  165. {
  166.     printf (WD_PROD_NAME " v%d.%02d installed (%s)\n", pVer->dwVer / 100, pVer->dwVer % 100, pVer->cVer);
  167. }
  168.  
  169. void Print_status(HANDLE hWD)
  170. {
  171.     WD_DEBUG debug;
  172.     BZERO (debug);
  173.     debug.dwCmd = DEBUG_STATUS;
  174.     WD_Debug (hWD, &debug);
  175.     printf ("Debug level (%d) %s, Debug sections (0x%08x) %s, Buffer size %d\n", 
  176.         debug.dwLevel, LevelToString(debug.dwLevel), debug.dwSection, SectionToString(debug.dwSection), debug.dwBufferSize);
  177. }
  178.  
  179. #if defined(VXWORKS)
  180.     int main (char *arg1, char *arg2, char *arg3 )
  181. #else
  182.     int main (int argc, char *argv[]) 
  183. #endif
  184. {
  185.     WD_VERSION verBuf;
  186.     DWORD debug_mode = 2;
  187.     HANDLE hWD = INVALID_HANDLE_VALUE;
  188.     
  189.     #if defined(VXWORKS)
  190.         int argc=1;
  191.         char *argv[4];
  192.  
  193.         argv[0] = "wddebug_main";
  194.  
  195.         if(arg1 != NULL)
  196.         {    argv[1] = arg1;
  197.             argc++;
  198.         }
  199.         if(arg2 != NULL)
  200.         {    argv[2] = arg2;
  201.             argc++;
  202.         }
  203.         if(arg3 != NULL)
  204.         {    argv[3] = arg3;
  205.             argc++;
  206.         }
  207.     #endif
  208.     hWD = WD_Open();
  209.     
  210.     if (hWD==INVALID_HANDLE_VALUE)
  211.     {
  212.         printf ("Error: " WD_PROD_NAME " device not installed.\n");
  213.         return EXIT_FAILURE;
  214.     }
  215.  
  216.     BZERO(verBuf);
  217.     WD_Version (hWD, &verBuf);
  218.  
  219.     if (argc<2)
  220.     {
  221.         Print_version (&verBuf);
  222.         printf ("\n");
  223.         Usage();
  224.         return EXIT_SUCCESS;
  225.    }
  226.  
  227.     if (verBuf.dwVer<WD_VER)
  228.     {
  229.         Print_version (&verBuf);
  230.         printf ("Please update the " WD_PROD_NAME " installed to v%d.%02d, or newer.\n", WD_VER / 100, WD_VER % 100);
  231.         WD_Close(hWD);
  232.         return EXIT_FAILURE;
  233.     }
  234.  
  235.     if (_stricmp(argv[1],"on")==0)
  236.     {
  237.         WD_DEBUG debug;
  238.  
  239.         BZERO (debug);
  240.         debug.dwCmd = DEBUG_SET_FILTER;
  241.         debug.dwLevel = D_ERROR;
  242.         debug.dwSection = (DWORD) S_ALL;
  243.  
  244.     
  245.         if (argc>2)
  246.         {
  247.             if (argc>4)
  248.             {
  249.                 printf ("Too many arguments\n");
  250.                 Usage();
  251.                 return EXIT_FAILURE;
  252.             }
  253.  
  254.             if (!LevelToDword(argv[2], &debug.dwLevel))
  255.             {
  256.                 printf ("invalid level name (%s)\n", argv[2]);
  257.                 Usage();
  258.                 return EXIT_FAILURE;
  259.             }
  260.  
  261.             if (argc==4 && !SectionToDword(argv[3], &debug.dwSection))
  262.             {
  263.                 printf ("invalid section name (%s)\n", argv[3]);
  264.                 Usage();
  265.                 return EXIT_FAILURE;
  266.             }
  267.         }
  268.  
  269.         WD_Debug (hWD, &debug);
  270.         Print_status (hWD);
  271.     }
  272.     else if (_stricmp(argv[1],"off")==0)
  273.     {
  274.         WD_DEBUG debug;
  275.  
  276.         if (argc>2)
  277.         {
  278.             printf ("Too many arguments\n");
  279.             Usage();
  280.             return EXIT_FAILURE;
  281.         }
  282.  
  283.         BZERO (debug);
  284.         debug.dwCmd = DEBUG_SET_FILTER;
  285.         debug.dwLevel = D_OFF;
  286.         debug.dwSection = 0;
  287.         WD_Debug (hWD, &debug);
  288.     }
  289.     else if (_stricmp(argv[1],"status")==0)
  290.     {
  291.         if (argc>2)
  292.         {
  293.             printf ("Too many arguments\n");
  294.             Usage();
  295.             return EXIT_FAILURE;
  296.         }
  297.  
  298.         Print_version (&verBuf);
  299.         Print_status (hWD);
  300.     }
  301.     else if (_stricmp(argv[1],"dump")==0)
  302.     {
  303.         WD_DEBUG_DUMP debugDump;
  304.         char sOSName[50]; 
  305.         char buf[2048];
  306.         time_t ltime;
  307.  
  308.         #if defined(UNIX) || defined(OS2)
  309.             int stdin_fileno = 
  310.             #if defined(VXWORKS)
  311.                 STD_IN;
  312.             #else
  313.                 STDIN_FILENO;
  314.             #endif
  315.         #elif defined(WIN32)
  316.             OSVERSIONINFO lVerInfo;
  317.         #endif
  318.  
  319.         if (argc>2)
  320.         {
  321.             printf ("Too many arguments\n");
  322.             Usage();
  323.             return EXIT_FAILURE;
  324.         }
  325.  
  326.         time(<ime);
  327.         #if defined(WIN32)
  328.             lVerInfo.dwOSVersionInfoSize = sizeof (lVerInfo);
  329.             GetVersionEx (&lVerInfo);
  330.         #endif
  331.  
  332.         printf ("WDDEBUG v%d.%02d Debugging Monitor.\n", WD_VER / 100, 
  333.             WD_VER % 100);
  334.         printf ("Running %s\n", verBuf.cVer);
  335.         #if !defined(WINCE)
  336.             printf ("Time: %s", ctime (<ime));
  337.         #endif
  338.         #if defined(WIN32)
  339.             switch (lVerInfo.dwPlatformId)
  340.             {
  341.             case VER_PLATFORM_WIN32_NT:
  342.                 strcpy(sOSName, "NT");
  343.                 break;
  344.             case VER_PLATFORM_WIN32_WINDOWS:
  345.                 if (lVerInfo.dwMinorVersion)
  346.                     strcpy(sOSName, "98");
  347.                 else
  348.                     strcpy(sOSName, "95");
  349.             break;
  350.             #if defined(VER_PLATFORM_WIN32_CE)
  351.                 case VER_PLATFORM_WIN32_CE:
  352.                     strcpy(sOSName, "CE");
  353.                     break;
  354.             #endif
  355.             default:
  356.                 strcpy(sOSName, "");
  357.             }
  358.             printf ("OS: Windows %s %d.%d Build %d.%d.%d %s\n",
  359.                 sOSName,
  360.                 lVerInfo.dwMajorVersion, lVerInfo.dwMinorVersion,
  361.                 lVerInfo.dwBuildNumber >> 24 & 0xff,
  362.                 lVerInfo.dwBuildNumber >> 16 & 0xff,
  363.                 lVerInfo.dwBuildNumber & 0xffff,
  364.                 lVerInfo.szCSDVersion);
  365.         #elif defined(VXWORKS)
  366.             printf("OS: VxWorks\n");
  367.         #elif defined(OS2)
  368.             printf("OS: OS/2\n");
  369.         #elif defined(LINUX)
  370.             printf("OS: Linux\n");
  371.         #elif defined(SOLARIS)
  372.             printf("OS: Solaris\n");
  373.         #else
  374.             printf("OS: Unknown\n");
  375.         #endif
  376.         #if defined(UNIX)
  377.             #if defined(VXWORKS)
  378.                 printf("Press CTRL-BREAK to exit\n");
  379.             #else
  380.                 printf("Press enter to exit\n");
  381.             #endif
  382.         #else
  383.             printf ("Press ESC to exit\n");
  384.         #endif
  385.         printf ("\n");
  386.         BZERO (debugDump);
  387.         debugDump.pcBuffer = buf;
  388.         debugDump.dwSize = sizeof (buf);
  389.         #if (defined(UNIX) && !defined(VXWORKS)) || defined(OS2)
  390.             fcntl(stdin_fileno, F_SETFL, fcntl(stdin_fileno, F_GETFL, 0) |
  391.                 O_NONBLOCK);
  392.         #endif
  393.         for (;;)
  394.         {
  395.             #if (defined(UNIX) && !defined(VXWORKS)) || defined(OS2)
  396.                 char buf[4];
  397.                 int nRead = read(stdin_fileno, buf, 4);
  398.                 if (nRead>0)
  399.                     break;
  400.             #elif defined(VXWORKS)
  401.                  // Will be implemented    
  402.             #elif defined(WIN32) && !defined(WINCE)
  403.                 if (_kbhit() && getch()==27) break;
  404.             #endif
  405.             do
  406.             {
  407.                 WD_DebugDump(hWD, &debugDump);
  408.                 printf ("%s", debugDump.pcBuffer);
  409.             } while (debugDump.pcBuffer[0]);
  410.             Sleep(100);
  411.         }
  412.     }
  413.     else
  414.     {
  415.         printf ("invalid option (%s)\n", argv[1]);
  416.         Usage();
  417.     }
  418.  
  419.     WD_Close (hWD);
  420.     return EXIT_SUCCESS;
  421. }
  422.  
  423.